from fastapi import FastAPI
from starlette.responses import HTMLResponse
import uvicorn
import os
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.faker import Faker
from pyecharts.charts import Gauge
from ping3 import ping
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts

##定义左右格式
html0='''
<html>
<head>
<meta http-equiv="refresh" content="10">
</head>
<body>
<div id="header" style="clear:both;text-align:center;">
<h2 style="margin-bottom:0;">网络延时大屏实时展示（毫秒）</h2></div>
<div id="container" style="width:1200px"></div>
<div id="left11" style="width:300px;heigth:250px;float:left;">{}</div>
<div id="left12" style="width:300px;heigth:250px;float:left;">{}</div>
<div id="left13" style="heigth:250px;width:300px;float:left">{}</div>
<div id="left14" style="heigth:250px;width:300px;float:left">{}</div>
<div id="left21" style="clear:both;width:300px;heigth:250px;float:left;">{}</div>
<div id="left22" style="width:300px;heigth:250px;float:left;">{}</div>
<div id="left23" style="heigth:250px;width:300px;float:left">{}</div>
<div id="left24" style="heigth:250px;width:300px;float:left">{}</div>
<div id="left31" style="clear:both;width:300px;heigth:250px;float:left;">{}</div>
<div id="left32" style="width:300px;heigth:250px;float:left;">{}</div>
<div id="left33" style="heigth:250px;width:300px;float:left">{}</div>
<div id="left34" style="heigth:250px;width:300px;float:left">{}</div>
<div id="footer" style="clear:both;text-align:center;">
XX中国IT团队</div>
</div>
</body>
</html>
'''
html1='''
<html>
<head>
<meta http-equiv="refresh" content="10">
</head>
<body>
<div id="header" style="clear:both;text-align:center;">
<h2 style="margin-bottom:0;">网络延时大屏实时展示（毫秒）</h2></div>
<div id="container" style="width:1200px"></div>
<div id="left11" style="width:300px;heigth:250px;float:left;">{}</div>
<div id="left12" style="width:300px;heigth:250px;float:left;">{}</div>

<div id="footer" style="clear:both;text-align:center;">
捷德中国IT团队</div>
</div>
</body>
</html>
'''

def Gauge_template(location):
    value=ping_host(location)
    d=Gauge(opts.InitOpts(width="300px",height="280px",animation_opts=opts.AnimationOpts(animation=False)),)
    d.add(series_name="网络延时:ms", max_=300,data_pair=[[location, value]],        split_number=5,

        axisline_opts=opts.AxisLineOpts(

            linestyle_opts=opts.LineStyleOpts(

                color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30

            )

        ),detail_label_opts=opts.LabelOpts(formatter="{value}"),)
    d.set_global_opts(

        legend_opts=opts.LegendOpts(is_show=False),

       

    )
    return d.render_embed()

def table():
    table = Table()

    headers = ["City name", "Area", "Population", "Annual Rainfall"]
    rows = [
        ["Brisbane", 5905, 1857594, 1146.4],
        ["Adelaide", 1295, 1158259, 600.5],
        ["Darwin", 112, 120900, 1714.7],
        ["Hobart", 1357, 205556, 619.5],
        ["Sydney", 2058, 4336374, 1214.8],
        ["Melbourne", 1566, 3806092, 646.9],
        ["Perth", 5386, 1554769, 869.4],
    ]
    table.add(headers, rows)
    table.set_global_opts(
        title_opts=ComponentTitleOpts(title="Table-基本示例", subtitle="我是副标题支持换行哦")
    )
    return table.render_embed()
def ping_host(ip):
    """
    获取节点的延迟的作用
    :param node:
    :return:
    """
    ip_address = ip
    response = ping(ip_address)
    #print(response)
    if response is not None:
        delay = int(response * 1000)
        #print(delay, "延迟")
        # 下面两行新增的
    else:
        return 500
    return delay

app = FastAPI()



@app.get("/")
def home1():
    v_nc=ping_host('www.baidu.com')
    v_hs=ping_host('114.114.114.114')
    return HTMLResponse(content=html0.format(Gauge_template('www.baidu.com'),Gauge_template('114.114.114.114'),Gauge_template('www.cnn.com'),Gauge_template('114.114.114.114'),\
                                            Gauge_template('www.baidu.com'),Gauge_template('114.114.114.114'),Gauge_template('www.cnn.com'),Gauge_template('114.114.114.114'),\
                                             Gauge_template('www.baidu.com'),Gauge_template('114.114.114.114'),Gauge_template('www.cnn.com'),Gauge_template('114.114.114.114'),
                                             ))
@app.get("/x")
def homex():

    return HTMLResponse(content=html1.format(Gauge_template('www.baidu.com'),table()
                                             ))



if __name__ == "__main__":

    print('当前程序名称为:',os.path.basename(__file__))
    filename=os.path.basename(__file__).split('.')[0]
    
    
    
    

    uvicorn.run(filename+":app", host="0.0.0.0",port=8000,log_level="info")
